#include "stm32f0xx.h"
#include "delay.h"
#include "led.h"
#include "USART1.h"
#include "PWM.h"
#include "stm32f0xx_adc.h"
#include "buzzer.h"

extern uint16_t ADC_ConvertedValue[2];
extern int end_flag;
enum
{
	sys_state_charge = 1,
	sys_state_charge_end = 2,
	sys_state_start = 3,
	sys_state_run = 4,
	sys_state_power_off = 5,	
};


void get_sys_status(int *sys_status_curr)
{
	uint16_t    dir=1;
	static int power_status_cnt = -30;
	static int charge_powerkey_status_cnt = -30;
	static int power_on_off_status = 0xFFFFFFFF;	
	static int charge_status_cnt = 0;		

	dir = get_charge_status();
	if(dir==1)
	{
		if(++charge_status_cnt>50)
		{
			*sys_status_curr = sys_state_charge;
			charge_status_cnt = 50;
			//power_on();
		}
		
	}
	{
		if((*sys_status_curr == sys_state_charge  ||*sys_status_curr == sys_state_charge_end)&&dir!=1)
		{
			*sys_status_curr = sys_state_charge_end;
			//return;			
		}
		if(Bit_SET == get_power_status())
		{
			power_status_cnt--;
			//LED_SET(LED_BLUE,LED_state_NONE);
		}
		else
		{
			power_status_cnt++;
			//LED_SET(LED_RED,LED_state_NONE);
		}
		if(power_status_cnt>10)
		{
			if(power_on_off_status != 0)
			{
				power_on();
				camera_fill_light_off();
				if(*sys_status_curr != sys_state_charge){
					*sys_status_curr = sys_state_start;
				}
			}
			else
			{
				camera_fill_light_off();
				if(*sys_status_curr != sys_state_charge){
					*sys_status_curr = sys_state_power_off;
				}else{
					camera_fill_light_on();
					power_off();
				}
//				power_off();
			}
			end_flag = 0;
		}
		if(power_status_cnt<0)
		{
			power_status_cnt = 0;
		}
		else if(power_status_cnt == 0)
		{
			power_on_off_status = ~power_on_off_status;
		}
		else if(power_status_cnt >200)
		{
			power_status_cnt = 200;
		}
	}
}

#define STAYUS_CHANGE_TIME	3000	//MS
int main(void)
{
	float  adcval=0,rec_adcval =3.3,adc_temp = 0;
	uint32_t	time_cnt=0,i,temp,Power_low_cnt,end_cnt = 0;
	int sys_status_curr = 0,rec_sys_status = 0;
	static int status_change_cnt = 2900,state_no = 0,bat_over_change_cnt = 0;
	static int power_off_cnt = 0;
	SystemInit();
	delay_init();
	LED_Init();
	ADC_CH_DMA_Config();
	TIM_Config();

	USART1_Init();
	BuzzInit();
//	delay_ms(100);
	for(i = 0;i<35;i++)
	{
		adc_temp = get_bat_vol();
		adcval = rec_adcval*0.9+adc_temp*0.1;
		rec_adcval = adcval;
	}
	while (1)
	{
		PlayMusic();
		delay_ms(1);
		Receive_poll();
		if(end_flag){
			end_cnt++;
			if(end_cnt>4000){
				power_off();
			}
		}else{
			end_cnt = 0;
		}
		time_cnt++;
		adcval = get_bat_vol();
		get_sys_status(&sys_status_curr);
		if(sys_status_curr != rec_sys_status)
		{
			state_no = 0;
		}
		rec_sys_status = sys_status_curr;
		camera_fill_light_off();
		//LED_SET(LED_BLUE,LED_state_NONE);
		switch(sys_status_curr)
		{
			case	sys_state_charge:
				//power_off();
				if(adcval<3.8)
				{
					status_change_cnt++;
					if(state_no==1)
					{
						status_change_cnt = 0;
					}
					else if(status_change_cnt>STAYUS_CHANGE_TIME &&state_no != 1&&state_no != 2&&state_no != 3)
					{
						state_no = 1;
					}
					else
					{}
				}
				else if(adcval<4.15)
				{
					status_change_cnt++;
					if(state_no==2)
					{
						status_change_cnt = 0;
					}
					else if(status_change_cnt>STAYUS_CHANGE_TIME &&state_no != 2&&state_no != 3)
					{
						state_no = 2;
					}
					else
					{}
					if(adcval>4.10)
					{
						bat_over_change_cnt++;
					}
					if(bat_over_change_cnt>10000*60)
					{
						state_no = 3;
					}
				}
				else
				{
					status_change_cnt++;
					if(state_no==3)
					{
						status_change_cnt = 0;
					}
					else if(status_change_cnt>STAYUS_CHANGE_TIME &&state_no != 3)
					{
						state_no = 3;
					}
					else
					{}
				}
				
				if(state_no == 0)
				{
					//LED_SET(LED_NONE,LED_state_NONE);
				}
				else if(state_no == 1)//power low
				{
					LED_SET(LED_RED,LED_breathe);//power mid
				}
				else if(state_no == 2)
				{
					LED_SET(LED_yellow,LED_breathe);//power full
				}
				else
				{
					LED_SET(LED_GREEN,LED_breathe);
				}
			break;
			case	sys_state_charge_end:
				camera_fill_light_off();
				sys_status_curr = sys_state_power_off;
			break;
			case	sys_state_start:
				//camera_fill_light_on();
				//LED_SET(LED_NONE,LED_state_NONE);
				if(time_cnt==100)
				{
					com_data[21]=0x55aa;
				}
				if(adcval<3.5)
				{
					//LED_SET(LED_RED,LED_state_NONE);
				}
				else if(adcval<3.9)
				{
					//LED_SET(LED_yellow,LED_state_NONE);
				}
				else
				{
					//LED_SET(LED_GREEN,LED_state_NONE);
				}
				if(time_cnt>10)
				{
					sys_status_curr = sys_state_run;
					//LED_SET(LED_BLUE,LED_state_NONE);
				}
			break;
			case	sys_state_run:
				//camera_fill_light_on();
				
				if(adcval<3.5)
				{
					Power_low_cnt++;
					status_change_cnt++;
					if(state_no==1)
					{
						status_change_cnt = 0;
					}
					else if(status_change_cnt>STAYUS_CHANGE_TIME &&state_no != 1)
					{
						state_no = 1;
					}
					else
					{}
					
					if(Power_low_cnt > 10000)
					{
						power_off();
						sys_status_curr = sys_state_power_off;
					}
				}else if(adcval<3.8)
				{
					status_change_cnt++;
					if(state_no==2)
					{
						status_change_cnt = 0;
					}
					else if(status_change_cnt>STAYUS_CHANGE_TIME &&state_no != 2)
					{
						state_no = 2;
					}
					else
					{}

					Power_low_cnt = 0;
				}else
				{
					status_change_cnt++;
					if(state_no==3)
					{
						status_change_cnt = 0;
					}
					else if(status_change_cnt>STAYUS_CHANGE_TIME &&state_no != 3&&state_no != 2)
					{
						state_no = 3;
					}
					else
					{}

					Power_low_cnt = 0;
				}

				if(state_no == 0)
				{
					//LED_SET(LED_BLUE,LED_breathe);
				}
				else if(state_no == 1)
				{
					//for(i=0;i<12;i++)
					{
						LED_SET(LED_RED,LED_state_NONE);
					}
				}
				else if(state_no == 2)
				{
					temp = time_cnt%500;
					if(temp<100)
					{
						LED_SET(LED_yellow,LED_state_NONE);
					}
					else
					{
						LED_SET(LED_yellow,LED_state_NONE);
					}
				}
				else if(state_no == 3)
				{
					LED_SET(LED_GREEN,LED_state_NONE);
				}
				else
				{
					LED_SET(LED_NONE,LED_state_NONE);
				}
			break;
			case	sys_state_power_off:
				power_off_cnt++;
				if(power_off_cnt==2)
				{
					com_data[22]=0x55aa;
				}
				if(power_off_cnt>500)
				{
					power_off();
				}
			break;
			default:
				LED_SET(LED_NONE,LED_state_NONE);
			break;
		}
		if(0)
		{
//			Senddata_to_esp32(0x5566,0x7788);
//			while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
		}
		PlayMusic();
	}
}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{

  while (1)
  {
  }
}
#endif

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
